package cc.wanforme.fsync.entity;

import cc.wanforme.fsync.util.LoggerTool;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FileInfo {
    private static final Logger log = LoggerTool.getLogger();

    // 名字
    private String name;
    // 路径
    private String path;
    // 路径
    private String absolutePath;
    // 是否是文件夹
    private boolean dir;
    // 大小
    private long size;

    private long createTime;
    private long modifyTime;

    public FileInfo(){}

    /** 列出文件夹的信息
     * path 为 null，表示所有根路径，
     * path 为 '/'， linux中表示根路径， windows 中表示java程序所在分区的根路径
     * */
    public static List<FileInfo> listFiles(String dirPath) {
        File[] files;
        if(dirPath == null) {
            files = File.listRoots();
        } else {
            File root = new File(dirPath);
            if(!root.exists()) {
                return null;
            }
            files = root.listFiles();
        }

        if(files == null) {
            return null;
        }

        List<FileInfo> list = Arrays.stream(files).map(FileInfo::from)
                .collect(Collectors.toList());
        return list;
    }

    /** 获取文件信息，不存在返回 null */
    public static FileInfo from(File file) {
        if(file == null || !file.exists()) {
            return null;
        }

        FileInfo info = new FileInfo();
        info.setName(file.getName());
        info.setPath(file.getPath());
        info.setAbsolutePath(file.getAbsolutePath());
        info.setDir(file.isDirectory());
        info.setSize(file.length());
        info.setModifyTime(file.lastModified());

        try {
            BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
            info.setSize(attrs.size());
            info.setCreateTime(attrs.creationTime().toMillis());
            info.setModifyTime(attrs.lastModifiedTime().toMillis());
        } catch (IOException e) {
            log.warn("Error: reading file's attributes", e);
        }

        return info;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getAbsolutePath() {
        return absolutePath;
    }

    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    public boolean isDir() {
        return dir;
    }

    public void setDir(boolean dir) {
        this.dir = dir;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public long getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(long modifyTime) {
        this.modifyTime = modifyTime;
    }

    public static void main(String[] args) {
        String path = "D:\\MyData\\ddd";
        FileInfo folder = FileInfo.from(new File(path + "\\控文件夹"));
        System.out.println(folder.getSize());
//        FileInfo file = FileInfo.from(new File(path + "\\空文件测试.txt"));
        FileInfo file = FileInfo.from(new File(path + "\\控文件夹\\新建文本文档.txt"));
        System.out.println(file.getSize());
    }
}
